package cn.itsource.controller.remote;


import cn.dev33.satoken.annotation.SaIgnore;

import cn.hutool.core.date.DateUtil;
import cn.itsource.api.openfeign.IRuleOpenfeignClient;

import cn.itsource.constants.RConst;
import cn.itsource.pojo.domain.ChargeRuleReturn;
import cn.itsource.pojo.domain.ChargeRuleStart;
import cn.itsource.pojo.dto.ExpectPriceDto;
import cn.itsource.pojo.ro.ExpectPriceRo;
import cn.itsource.result.JSONResult;
import cn.itsource.service.IChargeRuleReturnService;
import cn.itsource.service.IChargeRuleStartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;


@RestController

public class RuleOpenFeignClientImpl implements IRuleOpenfeignClient {
    @Autowired
    private IChargeRuleStartService chargeRuleStartService;
    @Autowired
    private IChargeRuleReturnService chargeRuleReturnService;

    @Override
    public JSONResult<ExpectPriceDto> expectPrice(@RequestBody ExpectPriceRo expectPriceRo) {
        //需要订单的创建时间
        Date orderTime = expectPriceRo.getOrderTime();
        //传过来的总公里数
        BigDecimal mileage = expectPriceRo.getMileage();
        //逻辑
        //得到订单的小时   4  11  22
        int hour = DateUtil.hour(orderTime, true);
        //判断 hour属于拿去区间
        ChargeRuleStart one = chargeRuleStartService
                .getOne(new LambdaQueryWrapper<ChargeRuleStart>()
                        .le(ChargeRuleStart::getHourStart, hour)
                        .gt(ChargeRuleStart::getHourEnd, hour));
        //传过来的公里数大于 才会进行查找
        ChargeRuleReturn byId = chargeRuleReturnService.getOne(new LambdaQueryWrapper<ChargeRuleReturn>()
                .lt(ChargeRuleReturn::getFreeBaseReturnMileage,mileage));

        //价格计算  去的钱
        //计算  预计价格
        BigDecimal baseMileage = one.getBaseMileage();
        //剩余里程
        BigDecimal subtract = mileage.subtract(baseMileage);
        //多余里程的价格
        BigDecimal multiply = subtract.multiply(one.getExceedEveryKmAmount());
        BigDecimal add = one.getAmount().add(multiply);//超出的价格 + 初始里程
        BigDecimal sumPrice = add;

        BigDecimal retSubtract = new BigDecimal(RConst.Num.INT_0);
        BigDecimal BigDecimal = new BigDecimal(RConst.Num.INT_0);

           if(byId!=null){
               //回来的钱
               //由总公里数减去基础返程公里数
               retSubtract = mileage.subtract(byId.getFreeBaseReturnMileage());
               //这是用超出来的公里数 去乘每公里超出价格
               BigDecimal = retSubtract.multiply(byId.getExceedEveryKmAmount());
               //总价格 及把回来的钱和去的钱相加
               sumPrice = sumPrice.add(BigDecimal);
           }
        ExpectPriceDto expectPriceDto = new ExpectPriceDto();
           expectPriceDto.setSumPrice(sumPrice);
        expectPriceDto.setBaseMileage(one.getBaseMileage());
        expectPriceDto.setBaseMileageAmount(one.getAmount());
        expectPriceDto.setExceedBaseMileageEveryKmAmount(one.getExceedEveryKmAmount());
        expectPriceDto.setExceedBaseMileage(subtract);
        expectPriceDto.setExceedBaseMileageAmount(multiply);
        expectPriceDto.setMileageAmount(add);

        expectPriceDto.setReturnMileage(expectPriceRo.getMileage());
        expectPriceDto.setFreeBaseReturnMileage(byId.getFreeBaseReturnMileage());
        expectPriceDto.setExceedBaseReturnEveryKmAmount(byId.getExceedEveryKmAmount());
        expectPriceDto.setExceedFreeBaseReturnMileage(retSubtract);
        expectPriceDto.setReturnAmont(BigDecimal);
        return JSONResult.success(expectPriceDto);
    }




}
